const fs = require('fs');
const path = require('path');

/* global obedient com object */

const defaultSettings = {
    comAffinity: [],
    entrysMax: 128,
    entrysDir: path.join(__dirname, 'dentry-files'),
    consoleDir: 'dconsole-logs',
};

const ocom = {
    /* system configuration file */
    settings: null,
    /* body pad */
    body: {
        doms: {
            info: null,
            infoMsg: null
        }
    },
    /* entry pad */
    entry: {
        doms: {
            entrys: null,
        }
    },
    /* config pad */
    config: {
        isopen: true,   /* indecate that the config panel is opened */
        doms: {
            config: null,
            configBar: null,
            configContent: null,
            com: { path: null, baudRate: null, dataBits: null, stopBits: null, parity: null },
            configComBtn: null,
            file: { entry: null, macro: null, script: null },
        },
    },
    /* console */
    console: {
        combinTime: 4000,
        forceCombin: false,  /* NOTE: i determin close it because of the performance */
        doms: {
            consoleMsgs: null,
            type: null
        }
    },
    /* com object */
    com: {
        isopen: false,
        serialport: null,
        recvData: null,
        dataDivTime: 50,        /* the timeout period for receiving data rows */
        // datgRecvHandler: null,  /* the data receive handler, a Promise callback */
    }
};

/* Initialization */

window.onload = function () {
    let index = 0;
    /* ocom object initial */
        /* system configuration file */
    try {
        ocom.settings = fs.readFileSync(path.join(__dirname, 'settings.json'), 'utf-8');
    } catch (error) {
        ocom.settings = defaultSettings;
    }
        /* body pad */
    ocom.body.doms.info = document.getElementById('info');
    ocom.body.doms.infoMsg = document.getElementById('info-msg');
        /* entry pad */
    ocom.entry.doms.entrys = document.getElementById('entrys');
        /* config pad */
    ocom.config.doms.config = document.getElementById('config');
    ocom.config.doms.configBar = document.getElementById('config-bar');
    ocom.config.doms.configContent = document.getElementById('config-content');
    ocom.config.doms.com.path = document.getElementById('path');
    ocom.config.doms.com.baudRate = document.getElementById('baudRate');
    ocom.config.doms.com.dataBits = document.getElementById('dataBits');
    ocom.config.doms.com.stopBits = document.getElementById('stopBits');
    ocom.config.doms.com.parity = document.getElementById('parity');
    ocom.config.doms.configComBtn = document.getElementById('config-com-btn');
    ocom.config.doms.file.entry = document.getElementById('config-file-entrys');
    ocom.config.doms.file.macro = document.getElementById('config-file-macro');
    ocom.config.doms.file.script = document.getElementById('config-file-script');
        /* console pad */
    ocom.console.doms.consoleMsgs = document.getElementById('console-msgs');
    ocom.console.doms.type = document.getElementById('settings-bar-console-type');
    /* generate entrys */
    if (ocom.config.doms.file.entry.value == 'null') {
        drawEntryPadByNum(ocom.entry.doms.entrys, ocom.settings.entrysMax);
    }

    /* Call other js */
    homeInit(index ++);
    animationInit(index ++);
    comBasicInit(index ++);

    /* test */
    // consoleAddS(ocom.console.doms.consoleMsgs, 'Title');
    // consoleAutoR(ocom.console.doms.consoleMsgs, 'Auto message');
    // consoleAddR(ocom.console.doms.consoleMsgs, 'Message');
    
    // document.getElementById('settings-bar').onclick = function (e) {
    //     consoleAutoR(ocom.console.doms.consoleMsgs, 'Auto message2');
    // }
    // document.getElementById('info').onclick = function (e) {
    //     consoleAddS(ocom.console.doms.consoleMsgs, 'New Title');
    // }
    // document.getElementById('config-file').onclick = function (e) {
    //     consoleAddR(ocom.console.doms.consoleMsgs, 'New Recv');
    // }
};

function isHex (str) {
    let ret = true;
    let reg = new RegExp('^[A-Fa-f0-9]{2}$');
    let arr = str.split(' ').filter((ele) => {
        return ele;   /* remove the empty string */
    });
    if (arr.length == 0) {
        return false;
    }
    arr.forEach((ele) => {
        if (!reg.test(ele)) {
            ret = false;
            return;
        }
    });
    return ret;
}
